#include "bsp_adc.h"


#define ADC_CTL1_CLB                    (0x1UL << 2)                             /*!< ADC calibration */
#define ADC_CTL1_RSTCLB                 (0x1UL << 3)                             /*!< reset calibration */
void adc_calibration_enable(void* adc_handler)
{
    volatile uint32_t timeout = 0;
    ADC_HandleTypeDef* hadc  = (ADC_HandleTypeDef*)adc_handler;

    ADC_Enable(hadc);

    while(1)
    {
        if(timeout++ > 18000)
        {
            timeout = 0;
             break;
        }
    }

    /* reset the selected ADC calibration registers */
    hadc->Instance->CR |= (uint32_t) ADC_CTL1_RSTCLB;
    /* check the RSTCLB bit state */
    while(RESET != (hadc->Instance->CR & ADC_CTL1_RSTCLB))
    {
        if(timeout++ > 18000)
        {
            timeout = 0;
            break;
        }
    }

    timeout = 0;
    /* enable ADC calibration process */
    hadc->Instance->CR |= ADC_CTL1_CLB;
    /* check the CLB bit state */
    while(RESET != (hadc->Instance->CR & ADC_CTL1_CLB)) {
        if(timeout++ > 18000)
        {
            timeout = 0;
            break;
        }
    }
}

void adc_start_dma(void *adc_handler, uint32_t *buf, uint32_t size)
{
    ADC_HandleTypeDef* hadc  = (ADC_HandleTypeDef*)adc_handler;
    HAL_ADC_Start_DMA(hadc, buf, size);
}
